package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: AddressBookController
 * Package: IntelliJ IDEA
 *
 * @author 张文豪
 * @description
 * @date 2023/8/18 19:15
 */
@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api("c端-地址接口")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    @ApiOperation("新增地址")
    @PostMapping
    public Result addAddress(@RequestBody AddressBook addressBook){
        log.info("新增地址：{}",addressBook);
        addressBookService.add(addressBook);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("获取当前用户的所有id")
    public Result<List<AddressBook>> listAddress(){
        log.info("当前用户id为：{}",BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list();
        return Result.success(list);
    }
    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result updateAddress(@RequestBody AddressBook addressBook){
        log.info("要修改的地址；{}",addressBook);
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteAddress(Long id){
        addressBookService.deleteAddress(id);
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getAddress(@PathVariable Long id){
        log.info("要查询地址的id为：{}",id);
        AddressBook addressBook = addressBookService.getAddress(id);
        return Result.success(addressBook);
    }
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("获取默认地址设置：{}",addressBook);
        addressBookService.setDefault(addressBook);
        return Result.success();
    }
    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> selectDefault(){
        log.info("查询默认：");
        AddressBook addressBook =  addressBookService.selectDefult();
        return Result.success(addressBook);
    }


}
